home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Tools / scripts / ndiff.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  23.8 KB  |  632 lines

  1. #! /usr/bin/env python
  2.  
  3. # Module ndiff version 1.4.0
  4. # Released to the public domain 27-Mar-1999,
  5. # by Tim Peters (tim_one@email.msn.com).
  6.  
  7. # Provided as-is; use at your own risk; no warranty; no promises; enjoy!
  8.  
  9. """ndiff [-q] file1 file2
  10.     or
  11. ndiff (-r1 | -r2) < ndiff_output > file1_or_file2
  12.  
  13. Print a human-friendly file difference report to stdout.  Both inter-
  14. and intra-line differences are noted.  In the second form, recreate file1
  15. (-r1) or file2 (-r2) on stdout, from an ndiff report on stdin.
  16.  
  17. In the first form, if -q ("quiet") is not specified, the first two lines
  18.  
  19. of output are
  20.  
  21. -: file1
  22. +: file2
  23.  
  24. Each remaining line begins with a two-letter code:
  25.  
  26.     "- "    line unique to file1
  27.     "+ "    line unique to file2
  28.     "  "    line common to both files
  29.     "? "    line not present in either input file
  30.  
  31. Lines beginning with "? " attempt to guide the eye to intraline
  32. differences, and were not present in either input file.  These lines can
  33.  
  34. be confusing if the source files contain tab characters.
  35.  
  36. The first file can be recovered by retaining only lines that begin with
  37. "  " or "- ", and deleting those 2-character prefixes; use ndiff with -r1.
  38.  
  39. The second file can be recovered similarly, but by retaining only "  "
  40. and "+ " lines; use ndiff with -r2; or, on Unix, the second file can be
  41. recovered by piping the output through
  42.  
  43.     sed -n '/^[+ ] /s/^..//p'
  44.  
  45. See module comments for details and programmatic interface.
  46. """
  47.  
  48. __version__ = 1, 4, 0
  49.  
  50. # SequenceMatcher tries to compute a "human-friendly diff" between
  51. # two sequences (chiefly picturing a file as a sequence of lines,
  52. # and a line as a sequence of characters, here).  Unlike e.g. UNIX(tm)
  53. # diff, the fundamental notion is the longest *contiguous* & junk-free
  54. # matching subsequence.  That's what catches peoples' eyes.  The
  55. # Windows(tm) windiff has another interesting notion, pairing up elements
  56. # that appear uniquely in each sequence.  That, and the method here,
  57. # appear to yield more intuitive difference reports than does diff.  This
  58. # method appears to be the least vulnerable to synching up on blocks
  59. # of "junk lines", though (like blank lines in ordinary text files,
  60. # or maybe "<P>" lines in HTML files).  That may be because this is
  61. # the only method of the 3 that has a *concept* of "junk" <wink>.
  62. #
  63. # Note that ndiff makes no claim to produce a *minimal* diff.  To the
  64. # contrary, minimal diffs are often counter-intuitive, because they
  65. # synch up anywhere possible, sometimes accidental matches 100 pages
  66. # apart.  Restricting synch points to contiguous matches preserves some
  67. # notion of locality, at the occasional cost of producing a longer diff.
  68. #
  69. # With respect to junk, an earlier version of ndiff simply refused to
  70. # *start* a match with a junk element.  The result was cases like this:
  71. #     before: private Thread currentThread;
  72. #     after:  private volatile Thread currentThread;
  73. # If you consider whitespace to be junk, the longest contiguous match
  74. # not starting with junk is "e Thread currentThread".  So ndiff reported
  75. # that "e volatil" was inserted between the 't' and the 'e' in "private".
  76. # While an accurate view, to people that's absurd.  The current version
  77. # looks for matching blocks that are entirely junk-free, then extends the
  78. # longest one of those as far as possible but only with matching junk.
  79. # So now "currentThread" is matched, then extended to suck up the
  80. # preceding blank; then "private" is matched, and extended to suck up the
  81. # following blank; then "Thread" is matched; and finally ndiff reports
  82. # that "volatile " was inserted before "Thread".  The only quibble
  83. # remaining is that perhaps it was really the case that " volatile"
  84. # was inserted after "private".  I can live with that <wink>.
  85. #
  86. # NOTE on junk:  the module-level names
  87. #    IS_LINE_JUNK
  88. #    IS_CHARACTER_JUNK
  89. # can be set to any functions you like.  The first one should accept
  90. # a single string argument, and return true iff the string is junk.
  91. # The default is whether the regexp r"\s*#?\s*$" matches (i.e., a
  92. # line without visible characters, except for at most one splat).
  93. # The second should accept a string of length 1 etc.  The default is
  94. # whether the character is a blank or tab (note: bad idea to include
  95. # newline in this!).
  96. #
  97. # After setting those, you can call fcompare(f1name, f2name) with the
  98. # names of the files you want to compare.  The difference report
  99. # is sent to stdout.  Or you can call main(args), passing what would
  100. # have been in sys.argv[1:] had the cmd-line form been used.
  101.  
  102. import string
  103. TRACE = 0
  104.  
  105. # define what "junk" means
  106. import re
  107.  
  108. def IS_LINE_JUNK(line, pat=re.compile(r"\s*#?\s*$").match):
  109.     return pat(line) is not None
  110.  
  111. def IS_CHARACTER_JUNK(ch, ws=" \t"):
  112.     return ch in ws
  113.  
  114. del re
  115.  
  116. class SequenceMatcher:
  117.     def __init__(self, isjunk=None, a='', b=''):
  118.         # Members:
  119.         # a
  120.         #      first sequence
  121.         # b
  122.         #      second sequence; differences are computed as "what do
  123.         #      we need to do to 'a' to change it into 'b'?"
  124.         # b2j
  125.         #      for x in b, b2j[x] is a list of the indices (into b)
  126.         #      at which x appears; junk elements do not appear
  127.         # b2jhas
  128.         #      b2j.has_key
  129.         # fullbcount
  130.         #      for x in b, fullbcount[x] == the number of times x
  131.         #      appears in b; only materialized if really needed (used
  132.         #      only for computing quick_ratio())
  133.         # matching_blocks
  134.         #      a list of (i, j, k) triples, where a[i:i+k] == b[j:j+k];
  135.         #      ascending & non-overlapping in i and in j; terminated by
  136.         #      a dummy (len(a), len(b), 0) sentinel
  137.         # opcodes
  138.         #      a list of (tag, i1, i2, j1, j2) tuples, where tag is
  139.         #      one of
  140.         #          'replace'   a[i1:i2] should be replaced by b[j1:j2]
  141.         #          'delete'    a[i1:i2] should be deleted
  142.         #          'insert'    b[j1:j2] should be inserted
  143.         #          'equal'     a[i1:i2] == b[j1:j2]
  144.         # isjunk
  145.         #      a user-supplied function taking a sequence element and
  146.         #      returning true iff the element is "junk" -- this has
  147.         #      subtle but helpful effects on the algorithm, which I'll
  148.         #      get around to writing up someday <0.9 wink>.
  149.         #      DON'T USE!  Only __chain_b uses this.  Use isbjunk.
  150.         # isbjunk
  151.         #      for x in b, isbjunk(x) == isjunk(x) but much faster;
  152.         #      it's really the has_key method of a hidden dict.
  153.         #      DOES NOT WORK for x in a!
  154.  
  155.         self.isjunk = isjunk
  156.         self.a = self.b = None
  157.         self.set_seqs(a, b)
  158.  
  159.     def set_seqs(self, a, b):
  160.         self.set_seq1(a)
  161.         self.set_seq2(b)
  162.  
  163.     def set_seq1(self, a):
  164.         if a is self.a:
  165.             return
  166.         self.a = a
  167.         self.matching_blocks = self.opcodes = None
  168.  
  169.     def set_seq2(self, b):
  170.         if b is self.b:
  171.             return
  172.         self.b = b
  173.         self.matching_blocks = self.opcodes = None
  174.         self.fullbcount = None
  175.         self.__chain_b()
  176.  
  177.     # For each element x in b, set b2j[x] to a list of the indices in
  178.     # b where x appears; the indices are in increasing order; note that
  179.     # the number of times x appears in b is len(b2j[x]) ...
  180.     # when self.isjunk is defined, junk elements don't show up in this
  181.     # map at all, which stops the central find_longest_match method
  182.     # from starting any matching block at a junk element ...
  183.     # also creates the fast isbjunk function ...
  184.     # note that this is only called when b changes; so for cross-product
  185.     # kinds of matches, it's best to call set_seq2 once, then set_seq1
  186.     # repeatedly
  187.  
  188.     def __chain_b(self):
  189.         # Because isjunk is a user-defined (not C) function, and we test
  190.         # for junk a LOT, it's important to minimize the number of calls.
  191.         # Before the tricks described here, __chain_b was by far the most
  192.         # time-consuming routine in the whole module!  If anyone sees
  193.         # Jim Roskind, thank him again for profile.py -- I never would
  194.         # have guessed that.
  195.         # The first trick is to build b2j ignoring the possibility
  196.         # of junk.  I.e., we don't call isjunk at all yet.  Throwing
  197.         # out the junk later is much cheaper than building b2j "right"
  198.         # from the start.
  199.         b = self.b
  200.         self.b2j = b2j = {}
  201.         self.b2jhas = b2jhas = b2j.has_key
  202.         for i in xrange(len(b)):
  203.             elt = b[i]
  204.             if b2jhas(elt):
  205.                 b2j[elt].append(i)
  206.             else:
  207.                 b2j[elt] = [i]
  208.  
  209.         # Now b2j.keys() contains elements uniquely, and especially when
  210.         # the sequence is a string, that's usually a good deal smaller
  211.         # than len(string).  The difference is the number of isjunk calls
  212.         # saved.
  213.         isjunk, junkdict = self.isjunk, {}
  214.         if isjunk:
  215.             for elt in b2j.keys():
  216.                 if isjunk(elt):
  217.                     junkdict[elt] = 1   # value irrelevant; it's a set
  218.                     del b2j[elt]
  219.  
  220.         # Now for x in b, isjunk(x) == junkdict.has_key(x), but the
  221.         # latter is much faster.  Note too that while there may be a
  222.         # lot of junk in the sequence, the number of *unique* junk
  223.         # elements is probably small.  So the memory burden of keeping
  224.         # this dict alive is likely trivial compared to the size of b2j.
  225.         self.isbjunk = junkdict.has_key
  226.  
  227.     def find_longest_match(self, alo, ahi, blo, bhi):
  228.         """Find longest matching block in a[alo:ahi] and b[blo:bhi].
  229.  
  230.         If isjunk is not defined:
  231.  
  232.         Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where
  233.             alo <= i <= i+k <= ahi
  234.             blo <= j <= j+k <= bhi
  235.         and for all (i',j',k') meeting those conditions,
  236.             k >= k'
  237.             i <= i'
  238.             and if i == i', j <= j'
  239.         In other words, of all maximal matching blocks, return one
  240.         that starts earliest in a, and of all those maximal matching
  241.         blocks that start earliest in a, return the one that starts
  242.         earliest in b.
  243.  
  244.         If isjunk is defined, first the longest matching block is
  245.         determined as above, but with the additional restriction that
  246.         no junk element appears in the block.  Then that block is
  247.         extended as far as possible by matching (only) junk elements on
  248.         both sides.  So the resulting block never matches on junk except
  249.         as identical junk happens to be adjacent to an "interesting"
  250.         match.
  251.  
  252.         If no blocks match, return (alo, blo, 0).
  253.         """
  254.  
  255.         # CAUTION:  stripping common prefix or suffix would be incorrect.
  256.         # E.g.,
  257.         #    ab
  258.         #    acab
  259.         # Longest matching block is "ab", but if common prefix is
  260.         # stripped, it's "a" (tied with "b").  UNIX(tm) diff does so
  261.         # strip, so ends up claiming that ab is changed to acab by
  262.         # inserting "ca" in the middle.  That's minimal but unintuitive:
  263.         # "it's obvious" that someone inserted "ac" at the front.
  264.         # Windiff ends up at the same place as diff, but by pairing up
  265.         # the unique 'b's and then matching the first two 'a's.
  266.  
  267.         a, b, b2j, isbjunk = self.a, self.b, self.b2j, self.isbjunk
  268.         besti, bestj, bestsize = alo, blo, 0
  269.         # find longest junk-free match
  270.         # during an iteration of the loop, j2len[j] = length of longest
  271.         # junk-free match ending with a[i-1] and b[j]
  272.         j2len = {}
  273.         nothing = []
  274.         for i in xrange(alo, ahi):
  275.             # look at all instances of a[i] in b; note that because
  276.             # b2j has no junk keys, the loop is skipped if a[i] is junk
  277.             j2lenget = j2len.get
  278.             newj2len = {}
  279.             for j in b2j.get(a[i], nothing):
  280.                 # a[i] matches b[j]
  281.                 if j < blo:
  282.                     continue
  283.                 if j >= bhi:
  284.                     break
  285.                 k = newj2len[j] = j2lenget(j-1, 0) + 1
  286.                 if k > bestsize:
  287.                     besti, bestj, bestsize = i-k+1, j-k+1, k
  288.             j2len = newj2len
  289.  
  290.         # Now that we have a wholly interesting match (albeit possibly
  291.         # empty!), we may as well suck up the matching junk on each
  292.         # side of it too.  Can't think of a good reason not to, and it
  293.         # saves post-processing the (possibly considerable) expense of
  294.         # figuring out what to do with it.  In the case of an empty
  295.         # interesting match, this is clearly the right thing to do,
  296.         # because no other kind of match is possible in the regions.
  297.         while besti > alo and bestj > blo and \
  298.               isbjunk(b[bestj-1]) and \
  299.               a[besti-1] == b[bestj-1]:
  300.             besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
  301.         while besti+bestsize < ahi and bestj+bestsize < bhi and \
  302.               isbjunk(b[bestj+bestsize]) and \
  303.               a[besti+bestsize] == b[bestj+bestsize]:
  304.             bestsize = bestsize + 1
  305.  
  306.         if TRACE:
  307.             print "get_matching_blocks", alo, ahi, blo, bhi
  308.             print "    returns", besti, bestj, bestsize
  309.         return besti, bestj, bestsize
  310.  
  311.     def get_matching_blocks(self):
  312.         if self.matching_blocks is not None:
  313.             return self.matching_blocks
  314.         self.matching_blocks = []
  315.         la, lb = len(self.a), len(self.b)
  316.         self.__helper(0, la, 0, lb, self.matching_blocks)
  317.         self.matching_blocks.append( (la, lb, 0) )
  318.         if TRACE:
  319.             print '*** matching blocks', self.matching_blocks
  320.         return self.matching_blocks
  321.  
  322.     # builds list of matching blocks covering a[alo:ahi] and
  323.     # b[blo:bhi], appending them in increasing order to answer
  324.  
  325.     def __helper(self, alo, ahi, blo, bhi, answer):
  326.         i, j, k = x = self.find_longest_match(alo, ahi, blo, bhi)
  327.         # a[alo:i] vs b[blo:j] unknown
  328.         # a[i:i+k] same as b[j:j+k]
  329.         # a[i+k:ahi] vs b[j+k:bhi] unknown
  330.         if k:
  331.             if alo < i and blo < j:
  332.                 self.__helper(alo, i, blo, j, answer)
  333.             answer.append(x)
  334.             if i+k < ahi and j+k < bhi:
  335.                 self.__helper(i+k, ahi, j+k, bhi, answer)
  336.  
  337.     def ratio(self):
  338.         """Return a measure of the sequences' similarity (float in [0,1]).
  339.  
  340.         Where T is the total number of elements in both sequences, and
  341.         M is the number of matches, this is 2*M / T.
  342.         Note that this is 1 if the sequences are identical, and 0 if
  343.         they have nothing in common.
  344.         """
  345.  
  346.         matches = reduce(lambda sum, triple: sum + triple[-1],
  347.                          self.get_matching_blocks(), 0)
  348.         return 2.0 * matches / (len(self.a) + len(self.b))
  349.  
  350.     def quick_ratio(self):
  351.         """Return an upper bound on ratio() relatively quickly."""
  352.         # viewing a and b as multisets, set matches to the cardinality
  353.         # of their intersection; this counts the number of matches
  354.         # without regard to order, so is clearly an upper bound
  355.         if self.fullbcount is None:
  356.             self.fullbcount = fullbcount = {}
  357.             for elt in self.b:
  358.                 fullbcount[elt] = fullbcount.get(elt, 0) + 1
  359.         fullbcount = self.fullbcount
  360.         # avail[x] is the number of times x appears in 'b' less the
  361.         # number of times we've seen it in 'a' so far ... kinda
  362.         avail = {}
  363.         availhas, matches = avail.has_key, 0
  364.         for elt in self.a:
  365.             if availhas(elt):
  366.                 numb = avail[elt]
  367.             else:
  368.                 numb = fullbcount.get(elt, 0)
  369.             avail[elt] = numb - 1
  370.             if numb > 0:
  371.                 matches = matches + 1
  372.         return 2.0 * matches / (len(self.a) + len(self.b))
  373.  
  374.     def real_quick_ratio(self):
  375.         """Return an upper bound on ratio() very quickly"""
  376.         la, lb = len(self.a), len(self.b)
  377.         # can't have more matches than the number of elements in the
  378.         # shorter sequence
  379.         return 2.0 * min(la, lb) / (la + lb)
  380.  
  381.     def get_opcodes(self):
  382.         if self.opcodes is not None:
  383.             return self.opcodes
  384.         i = j = 0
  385.         self.opcodes = answer = []
  386.         for ai, bj, size in self.get_matching_blocks():
  387.             # invariant:  we've pumped out correct diffs to change
  388.             # a[:i] into b[:j], and the next matching block is
  389.             # a[ai:ai+size] == b[bj:bj+size].  So we need to pump
  390.             # out a diff to change a[i:ai] into b[j:bj], pump out
  391.             # the matching block, and move (i,j) beyond the match
  392.             tag = ''
  393.             if i < ai and j < bj:
  394.                 tag = 'replace'
  395.             elif i < ai:
  396.                 tag = 'delete'
  397.             elif j < bj:
  398.                 tag = 'insert'
  399.             if tag:
  400.                 answer.append( (tag, i, ai, j, bj) )
  401.             i, j = ai+size, bj+size
  402.             # the list of matching blocks is terminated by a
  403.             # sentinel with size 0
  404.             if size:
  405.                 answer.append( ('equal', ai, i, bj, j) )
  406.         return answer
  407.  
  408. # meant for dumping lines
  409. def dump(tag, x, lo, hi):
  410.     for i in xrange(lo, hi):
  411.         print tag, x[i],
  412.  
  413. # figure out which mark to stick under characters in lines that
  414. # have changed (blank = same, - = deleted, + = inserted, ^ = replaced)
  415. _combine = { '  ': ' ',
  416.              '. ': '-',
  417.              ' .': '+',
  418.              '..': '^' }
  419.  
  420. def plain_replace(a, alo, ahi, b, blo, bhi):
  421.     assert alo < ahi and blo < bhi
  422.     # dump the shorter block first -- reduces the burden on short-term
  423.     # memory if the blocks are of very different sizes
  424.     if bhi - blo < ahi - alo:
  425.         dump('+', b, blo, bhi)
  426.         dump('-', a, alo, ahi)
  427.     else:
  428.         dump('-', a, alo, ahi)
  429.         dump('+', b, blo, bhi)
  430.  
  431. # When replacing one block of lines with another, this guy searches
  432. # the blocks for *similar* lines; the best-matching pair (if any) is
  433. # used as a synch point, and intraline difference marking is done on
  434. # the similar pair.  Lots of work, but often worth it.
  435.  
  436. def fancy_replace(a, alo, ahi, b, blo, bhi):
  437.     if TRACE:
  438.         print '*** fancy_replace', alo, ahi, blo, bhi
  439.         dump('>', a, alo, ahi)
  440.         dump('<', b, blo, bhi)
  441.  
  442.     # don't synch up unless the lines have a similarity score of at
  443.     # least cutoff; best_ratio tracks the best score seen so far
  444.     best_ratio, cutoff = 0.74, 0.75
  445.     cruncher = SequenceMatcher(IS_CHARACTER_JUNK)
  446.     eqi, eqj = None, None   # 1st indices of equal lines (if any)
  447.  
  448.     # search for the pair that matches best without being identical
  449.     # (identical lines must be junk lines, & we don't want to synch up
  450.     # on junk -- unless we have to)
  451.     for j in xrange(blo, bhi):
  452.         bj = b[j]
  453.         cruncher.set_seq2(bj)
  454.         for i in xrange(alo, ahi):
  455.             ai = a[i]
  456.             if ai == bj:
  457.                 if eqi is None:
  458.                     eqi, eqj = i, j
  459.                 continue
  460.             cruncher.set_seq1(ai)
  461.             # computing similarity is expensive, so use the quick
  462.             # upper bounds first -- have seen this speed up messy
  463.             # compares by a factor of 3.
  464.             # note that ratio() is only expensive to compute the first
  465.             # time it's called on a sequence pair; the expensive part
  466.             # of the computation is cached by cruncher
  467.             if cruncher.real_quick_ratio() > best_ratio and \
  468.                   cruncher.quick_ratio() > best_ratio and \
  469.                   cruncher.ratio() > best_ratio:
  470.                 best_ratio, best_i, best_j = cruncher.ratio(), i, j
  471.     if best_ratio < cutoff:
  472.         # no non-identical "pretty close" pair
  473.         if eqi is None:
  474.             # no identical pair either -- treat it as a straight replace
  475.             plain_replace(a, alo, ahi, b, blo, bhi)
  476.             return
  477.         # no close pair, but an identical pair -- synch up on that
  478.         best_i, best_j, best_ratio = eqi, eqj, 1.0
  479.     else:
  480.         # there's a close pair, so forget the identical pair (if any)
  481.         eqi = None
  482.  
  483.     # a[best_i] very similar to b[best_j]; eqi is None iff they're not
  484.     # identical
  485.     if TRACE:
  486.         print '*** best_ratio', best_ratio, best_i, best_j
  487.         dump('>', a, best_i, best_i+1)
  488.         dump('<', b, best_j, best_j+1)
  489.  
  490.     # pump out diffs from before the synch point
  491.     fancy_helper(a, alo, best_i, b, blo, best_j)
  492.  
  493.     # do intraline marking on the synch pair
  494.     aelt, belt = a[best_i], b[best_j]
  495.     if eqi is None:
  496.         # pump out a '-', '+', '?' triple for the synched lines;
  497.         atags = btags = ""
  498.         cruncher.set_seqs(aelt, belt)
  499.         for tag, ai1, ai2, bj1, bj2 in cruncher.get_opcodes():
  500.             la, lb = ai2 - ai1, bj2 - bj1
  501.             if tag == 'replace':
  502.                 atags = atags + '.' * la
  503.                 btags = btags + '.' * lb
  504.             elif tag == 'delete':
  505.                 atags = atags + '.' * la
  506.             elif tag == 'insert':
  507.                 btags = btags + '.' * lb
  508.             elif tag == 'equal':
  509.                 atags = atags + ' ' * la
  510.                 btags = btags + ' ' * lb
  511.             else:
  512.                 raise ValueError, 'unknown tag ' + `tag`
  513.         la, lb = len(atags), len(btags)
  514.         if la < lb:
  515.             atags = atags + ' ' * (lb - la)
  516.         elif lb < la:
  517.             btags = btags + ' ' * (la - lb)
  518.         combined = map(lambda x,y: _combine[x+y], atags, btags)
  519.         print '-', aelt, '+', belt, '?', \
  520.               string.rstrip(string.join(combined, ''))
  521.     else:
  522.         # the synch pair is identical
  523.         print ' ', aelt,
  524.  
  525.     # pump out diffs from after the synch point
  526.     fancy_helper(a, best_i+1, ahi, b, best_j+1, bhi)
  527.  
  528. def fancy_helper(a, alo, ahi, b, blo, bhi):
  529.     if alo < ahi:
  530.         if blo < bhi:
  531.             fancy_replace(a, alo, ahi, b, blo, bhi)
  532.         else:
  533.             dump('-', a, alo, ahi)
  534.     elif blo < bhi:
  535.         dump('+', b, blo, bhi)
  536.  
  537. def fail(msg):
  538.     import sys
  539.     out = sys.stderr.write
  540.     out(msg + "\n\n")
  541.     out(__doc__)
  542.     return 0
  543.  
  544. # open a file & return the file object; gripe and return 0 if it
  545. # couldn't be opened
  546. def fopen(fname):
  547.     try:
  548.         return open(fname, 'r')
  549.     except IOError, detail:
  550.         return fail("couldn't open " + fname + ": " + str(detail))
  551.  
  552. # open two files & spray the diff to stdout; return false iff a problem
  553. def fcompare(f1name, f2name):
  554.     f1 = fopen(f1name)
  555.     f2 = fopen(f2name)
  556.     if not f1 or not f2:
  557.         return 0
  558.  
  559.     a = f1.readlines(); f1.close()
  560.     b = f2.readlines(); f2.close()
  561.  
  562.     cruncher = SequenceMatcher(IS_LINE_JUNK, a, b)
  563.     for tag, alo, ahi, blo, bhi in cruncher.get_opcodes():
  564.         if tag == 'replace':
  565.             fancy_replace(a, alo, ahi, b, blo, bhi)
  566.         elif tag == 'delete':
  567.             dump('-', a, alo, ahi)
  568.         elif tag == 'insert':
  569.             dump('+', b, blo, bhi)
  570.         elif tag == 'equal':
  571.             dump(' ', a, alo, ahi)
  572.         else:
  573.             raise ValueError, 'unknown tag ' + `tag`
  574.  
  575.     return 1
  576.  
  577. # crack args (sys.argv[1:] is normal) & compare;
  578. # return false iff a problem
  579.  
  580. def main(args):
  581.     import getopt
  582.     try:
  583.         opts, args = getopt.getopt(args, "qr:")
  584.     except getopt.error, detail:
  585.         return fail(str(detail))
  586.     noisy = 1
  587.     qseen = rseen = 0
  588.     for opt, val in opts:
  589.         if opt == "-q":
  590.             qseen = 1
  591.             noisy = 0
  592.         elif opt == "-r":
  593.             rseen = 1
  594.             whichfile = val
  595.     if qseen and rseen:
  596.         return fail("can't specify both -q and -r")
  597.     if rseen:
  598.         if args:
  599.             return fail("no args allowed with -r option")
  600.         if whichfile in "12":
  601.             restore(whichfile)
  602.             return 1
  603.         return fail("-r value must be 1 or 2")
  604.     if len(args) != 2:
  605.         return fail("need 2 filename args")
  606.     f1name, f2name = args
  607.     if noisy:
  608.         print '-:', f1name
  609.         print '+:', f2name
  610.     return fcompare(f1name, f2name)
  611.  
  612. def restore(which):
  613.     import sys
  614.     tag = {"1": "- ", "2": "+ "}[which]
  615.     prefixes = ("  ", tag)
  616.     for line in sys.stdin.readlines():
  617.         if line[:2] in prefixes:
  618.             print line[2:],
  619.  
  620. if __name__ == '__main__':
  621.     import sys
  622.     args = sys.argv[1:]
  623.     if "-profile" in args:
  624.         import profile, pstats
  625.         args.remove("-profile")
  626.         statf = "ndiff.pro"
  627.         profile.run("main(args)", statf)
  628.         stats = pstats.Stats(statf)
  629.         stats.strip_dirs().sort_stats('time').print_stats()
  630.     else:
  631.         main(args)
  632.